package edu.uic.readfile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class AllWord
{
	public TreeMap<String, Integer> wordvector = new TreeMap();
	public HashSet<String> wordHashSet = new HashSet();
	public HashMap<String, Integer> wordHashMap = new HashMap();
	
	private String filepath = "eocnomywordlist.txt";
	
	public void readfromFile()
	{
		try
		{
			BufferedReader br = new BufferedReader(new FileReader(filepath));
			String templine = br.readLine();
			String word = templine.trim();
			int size = 0;
			while(templine != null)
			{
				word = templine.trim();
				if(!wordHashSet.contains(word))
				{
					wordHashSet.add(word);
					size = wordHashSet.size();
					wordvector.put(word, size);
					
				}
				
				templine = br.readLine();
			}
			br.close();
			
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void savetoFile()
	{
		try
		{
			BufferedWriter bw = new BufferedWriter(new FileWriter(filepath));
			for(String word : wordHashMap.keySet())
			{
				if(wordHashMap.get(word) > 2)
				{
					bw.write(word);
					bw.newLine();
				}
					
			}
			
			bw.close();
			
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	public void addword(String word)
	{
		//int size = 0;
		if(!wordHashMap.containsKey(word))
		{
			wordHashMap.put(word, 1);
		}
		else
		{
			int times = wordHashMap.get(word);
			times++;
			wordHashMap.put(word, times);
		}
	}
	
	
	public int wordIndex(String word)
	{
		int index = 0;
		index = wordvector.get(word);
		return index;
	}
}
